<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2022-08-25 21:33:51
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Models;

use App\Traits\StorageTrait;

class OpenCardDetails extends BaseModel
    {
    use StorageTrait;
    public $timestamps = true;
    public $table = "sys_open_card_details";
    protected $guarded = [];

    public function main()
        {
        return $this->belongsTo(OpenCard::class);
        }
    public function process_pass()
        {
        return $this->hasMany(ProProcessesPass::class, "open_card_id");
        }
    public function parameter_testing()
        {
        return $this->hasMany(OpenCardDetailsParameterTesting::class, "open_card_id");
        }

    public function scheduling()
        {
        return $this->hasMany(OpenCardDetailsScheduling::class, "open_card_details_id");
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function route_process()
        {
        return $this->belongsTo(ProProcessRouteDetails::class);
        }
    public function processrename()
        {
        return $this->belongsTo(ProProcessRouteDetails::class, 'route_process_id');
        }
    public function shell_fabric()
        {
        return $this->hasOne(OpenCardShellFabric::class, "main_id");
        }
    public function getACCode()
        {
        return $this->main->code;
        }
    public function craft_category()
        {
        return $this->belongsTo(Dic::class, "craft_category_id", "key")->where(function ($q)
            {
            $q->where("code", 'CraftCategory');
            });
        }

    public function inspect()
        {
        return $this->hasOne(ProProcessesInspect::class, "open_card_id");
        }

    public function film_category()
        {
        return $this->belongsTo(Dic::class, "film_category_id", "key")->where(function ($q)
            {
            $q->where("code", 'FilmCategory');
            });
        }

    public function process_route()
        {
        return $this->belongsTo(ProProcessRoute::class);
        }
    public function wip()
        {
        return $this->hasMany(ProProcessesWip::class, "open_card_id");
        }
    public function storagestatus()
        {
        return $this->belongsTo(Dic::class, "storagestatus_id", "key")->where(function ($q)
            {
            $q->where("code", 'storagestatus_name');
            });
        }
    public function order()
        {
        return $this->belongsTo(CustomerStorageDetail::class, "batchno", "batchno");
        }
    public function machine()
        {
        return $this->belongsTo(Loom::class);
        }

    public function washed_index()
        {
        return $this->belongsTo(Dic::class, "washed_index_id", "key")->where(function ($q)
            {
            $q->where("code", 'washed_index');
            });
        }
    public function strip_index()
        {
        return $this->belongsTo(Dic::class, "strip_index_id", "key")->where(function ($q)
            {
            $q->where("code", 'strip_index');
            });
        }
    public function teststatus()
        {
        return $this->belongsTo(Dic::class, "teststatus_id", "key")->where(function ($q)
            {
            $q->where("code", 'checkResult');
            });
        }
    public function parameter_testing_ts()
        {
        return $this->hasOne(OpenCardDetailsParameterTesting::class, "open_card_id")->where("name", '透湿指标');
        }
    public function parameter_testing_sy()
        {
        return $this->hasOne(OpenCardDetailsParameterTesting::class, "open_card_id")->where("name", '水压指标');
        }
    public function parameter_testing_bl()
        {
        return $this->hasOne(OpenCardDetailsParameterTesting::class, "open_card_id")->where("name", '剥离指标');
        }
    public function testor()
        {
        return $this->belongsTo(Employee::class);
        }
    public function updateRoll_quantity()
        {
        $this->roll_quantity = $this->wip->where("process.name", '倒卷')->sum(function ($v)
            {
            //direction_id 返工过数，相当于负数
            return $v->faultle_quantity * $v->direction_id;
            });

        //倒卷损耗率
        $this->lost_roll_rate = ($this->quantity - $this->roll_quantity) / $this->quantity;
        $this->save();
        $this->order->updateRoll_quantity();
        }
    public function updateComposite_quantity($model)
        {
        if ($model->direction_id == 1)
            $this->composite_quantity = $model->faultle_quantity;
        $this->save();
        }

    public function cancel()
    {
        return $this->belongsTo(Dic::class, "is_cancel", "key")->where(function ($q) {
            $q->where("code", 'cancel_name');
        });
    }

    public function pass_last()
    {
        return $this->hasOne(ProProcessesPass::class, "open_card_id")->latest('id');
    }

    }
